// pages/my/myClass/myClass.js
const app = getApp()
var base_url = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    student: { 'name': '暂无', 'class': '暂无', 'school': '暂无' },
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    },
    mengban:'none'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({

      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        that.setData({
          height: clientHeight
        });
      }
    });
    /**
     * 获取用户信息
     */
    wx.getStorage({
      key: 'student_id',
      success: function (res) {
        that.setData({
          student_id: res.data
        })
      },
      fail: function (res) {

      }
    })
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        })
      }
    })

    wx.getStorage({
      key: 'class_dic',
      success: function (resl) {
        that.setData({
          student:resl.data
        })
        // wx.request({
        //   url: 'http://127.0.0.1:8000/getVisClassInfo/',
        //   method: 'POST',
        //   data: {
        //     student_id: resl.data
        //   },
        //   header: {
        //     "Content-Type": "application/x-www-form-urlencoded"
        //   },
        //   success: function (res) {
        //     if(res.data['flag'] == true){
        //       that.setData({
        //         student:res.data['class_dic']
        //       })
        //     }else{
        //       wx.navigateTo({
        //         url: '../addClass/addClass',
        //         success: function (res) {
        //           console.log(res)
        //         },
        //         fail: function () {

        //         }
        //       })
        //     }
        //   }
        // })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  lookClassWork: function () {
    wx.navigateTo({
      url: '../lookClassWork/lookClassWork',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
  },
  quitClass:function(){
    var that = this
    // this.setData({
    //   mengban:"block"
    // })
    
    wx.showModal({
      title: '您确定退出班级吗？',
      content: '注：请提前告知老师',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: base_url + '/quitClass/',
            method: 'POST',
            data: {
              student_id: that.data.student_id
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              wx.redirectTo({
                url: '../addClass/addClass',
                success: function (res) {
                  console.log(res)
                },
                fail: function () {
                }
              })
            }
          })
        } else {
          console.log('弹框后点取消')
        }
      }
    })
  },
  back: function () {
    this.setData({
      mengban: "none"
    })
  },
  sure: function () {
    var that = this
    that.setData({
      mengban: "none"
    })
    wx.request({
      url: base_url + '/quitClass/',
      method: 'POST',
      data: {
        student_id: that.data.student_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        wx.redirectTo({
          url: '../addClass/addClass',
          success: function (res) {
            console.log(res)
          },
          fail: function () {
          }
        })
      }
    })
  }
})